package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Ciudad;
import com.liwalo.entities.Pais;
import com.liwalo.entities.Provincia;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Ciudad.
 * @see com.liwalo.dao.Ciudad
 * @author Hibernate Tools
 */
public class CiudadDAO {

	private static final Log log = LogFactory.getLog(CiudadDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public void persist(Ciudad transientInstance) {
		log.debug("persisting Ciudad instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Ciudad instance) {
		log.debug("attaching dirty Ciudad instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Ciudad instance) {
		log.debug("attaching clean Ciudad instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Ciudad persistentInstance) {
		log.debug("deleting Ciudad instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Ciudad merge(Ciudad detachedInstance) {
		log.debug("merging Ciudad instance");
		try {
			Ciudad result = (Ciudad) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Ciudad findById(java.lang.Integer id) {
		log.debug("getting Ciudad instance with id: " + id);
		try {
			Ciudad instance = (Ciudad) sessionFactory.getCurrentSession().get(
					Ciudad.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Ciudad> FindByIdCiudad(Ciudad instance) {
		log.debug("finding Ciudad instance by example");
		try {
			List<Ciudad> results = (List<Ciudad>) sessionFactory
					.getCurrentSession()
					.createCriteria(Ciudad.class)
//					.createAlias("provinciaId", "provincia")
					.add(Restrictions.eq("provincia.id", instance.getProvincia().getId()))
					.add(Restrictions.eq("estado",1))
					.list();
					
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Ciudad> findByExample(Provincia instance) {
		log.debug("finding Ciudad instance by example");
		try {
			List<Ciudad> results = (List<Ciudad>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.Provincia")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Ciudad> getAllByProvincia(Provincia instance) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Ciudad.class)
								   .add(Restrictions.eq("provincia.id", instance.getId()))
								   .add(Restrictions.eq("estado",1));
		return (List<Ciudad>) criteria.list();
	}
}
